package chamans
{
	import flash.display.Sprite
		import flash.events.Event
		import flash.utils.getTimer
		import flash.text.StyleSheet
		import flash.system.System
		
		public class SYS_stats extends Sprite
		{	
			private var BASE:Object
			private var xml:XML
			private var xml0:XML
			private var xml2:XML
			public var TEXTE:SYS_text
			
			private var timer 			: uint;
			private var fps 			: uint;
			private var ms 				: uint;
			private var ms_prev 		: uint;
			private var mem 			: Number=0;
			private var mem_max 		: Number=0;
			
			public function SYS_stats(base:* , CSS:StyleSheet) : void
			{
				BASE=base
				mem_max = 0;
				TEXTE=new SYS_text(CSS, 190);
				xml=<xml>
				<iconf>f</iconf><fps></fps>
				<icont></icont><trigo></trigo>
				<iconm>x</iconm><memo></memo>
				<iconms></iconms><ms></ms>
				<iconi></iconi><info></info>
				</xml>;
				xml.icont=" "; xml.trigo =" ";
				xml.iconms=" "; xml.ms =" ";
				xml.iconi=" ";	xml.info=" ";	
				TEXTE.y=-33; TEXTE.x=-215;
				addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
				addEventListener(Event.REMOVED_FROM_STAGE, destroy, false, 0, true);
			}
			private function init(e : Event) : void
			{		
				addChild(TEXTE);
				addEventListener(Event.ENTER_FRAME, update);
			}
			private function update(e : Event) : void
			{
				timer = getTimer();
				if( timer - 1000 > ms_prev )
				{
					ms_prev = timer;
					mem = (System.totalMemory / Math.pow( ( 1 << 10 ), 2 )<<0);//mb
					fps = fps > stage.frameRate ? stage.frameRate : fps;
					xml.fps = fps;
					if(BASE.Engine=="away3d" && BASE.act){	
						xml.icont="p";
						xml.trigo = BASE.info();
					}
					else {}
					if (BASE.physicEngine=="jiglib" || BASE.physicEngine=="bullet"){
						xml.iconi="g";
						xml.info = BASE.physics.info();
					}
					xml.memo =  mem+"mb";
					TEXTE.readXML(xml);
					fps = 0;
				}
				fps++;
				ms = timer;
			}
			public function destroy(event : Event=null) : void
			{
				while(numChildren > 0) removeChildAt(0);			
				removeEventListener(Event.ENTER_FRAME, update);
			}		
			
		}}